#include<bits/stdc++.h>
using namespace std;
int n, q;
struct Node {
    Node* pre;
    string seg;
    int len;
    Node(Node* p, const string& s) : pre(p), seg(s), len((p ? p->len : 0) + s.size()) {}
};
void solve() {
    cin >> n >> q;
    Node* e = new Node(nullptr, "");
    vector<Node*> pc(n + 1, e);          // PC
    Node* sev = e;                     // 服务器
    while (q--) {
        int tp;
        cin >> tp;
        if (tp == 1) {
            int p; cin >> p;
            pc[p] = sev;
        }
        else if (tp == 2) {
            int p; string s;
            cin >> p >> s;
            pc[p] = new Node(pc[p], s);
        }
        else {
            int p; cin >> p;
            sev = pc[p];
        }
    }
    vector<string> ans;         // 收集
    for (Node* cur = sev; cur != e; cur = cur->pre) ans.push_back(cur->seg);
    int l = ans.size();
    for (int i = l - 1; i >= 0; i--) cout << ans[i];
    cout << endl;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int T = 1;
    //cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}